﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Infrastructure.Extend
{
    /// <summary>
    /// String类扩展
    /// <remarks></remarks>
    /// </summary>
    public static class StringExtend
    {
        /// <summary>
        /// 如果为null则返回String.Empty
        /// </summary>
        /// <param name="source">源</param>
        /// <returns></returns>
        public static string NullThenEmpty(this string source)
        {
            if (source == null)
            {
                return string.Empty;
            }
            return source;
        }

        /// <summary>
        /// 判断字符串是否为null或Empty
        /// </summary>
        /// <param name="source">源</param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string source)
        {
            return string.IsNullOrEmpty(source);
        }

        /// <summary>
        /// 转换为整数
        /// 失败返回默认值0
        /// </summary>
        /// <param name="source">源</param>
        /// <returns></returns>
        public static int ToInt32(this string source)
        {
            int value = 0;
            int.TryParse(source, out value);
            return value;
        }

        /// <summary>
        /// 转换为整数
        /// 失败则直接返回默认值default
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="defalut">默认值</param>
        /// <returns></returns>
        public static int ToInt32(this string source, int defalut)
        {
            int value = 0;
            if (int.TryParse(source, out value))
            {
                return value;
            }
            return defalut;
        }

        /// <summary>
        /// 转换为GUID
        /// 失败则返回GUID.Empty
        /// </summary>
        /// <param name="source">源</param>
        /// <returns></returns>
        public static Guid ToGuid(this string source)
        {
            var value = Guid.Empty;
            Guid.TryParse(source, out value);
            return value;
        }

        /// <summary>
        /// 是否匹配正则表达式参数
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="pattert">regex参数</param>
        /// <returns></returns>
        public static bool IsMatch(this string source, string pattert)
        {
            source = source.NullThenEmpty();
            pattert = pattert.NullThenEmpty();
            return System.Text.RegularExpressions.Regex.IsMatch(source, pattert);
        }

        /// <summary>
        /// 匹配正则表示式
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="pattert">regex参数</param>
        /// <returns></returns>
        public static string Match(this string source, string pattert)
        {
            source = source.NullThenEmpty();
            pattert = pattert.NullThenEmpty();
            return System.Text.RegularExpressions.Regex.Match(source, pattert).Value;
        }

        /// <summary>
        /// 匹配正则表示式
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="pattert">regex参数</param>
        /// <returns></returns>
        public static string[] Matches(this string source, string pattert)
        {
            source = source.NullThenEmpty();
            pattert = pattert.NullThenEmpty();
            var mc = System.Text.RegularExpressions.Regex.Matches(source, pattert);
            string[] list = new string[mc.Count];
            int i = 0;
            foreach (System.Text.RegularExpressions.Match m in mc)
            {
                list[i] = m.Value;
                i++;
            }
            return list;
        }

        /// <summary>
        /// 重复填充字符
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="totalLength">填充后的长度</param>
        /// <returns></returns>
        public static string Pad(this char source, int totalLength)
        {
            return string.Empty.PadRight(totalLength, source);
        }

        /// <summary>
        /// 以GB2312编码获取字符串的长度
        /// 一个中文占两个长度 英文一个长度
        /// </summary>
        /// <param name="source">源</param>
        /// <returns></returns>
        public static int LengthGB2312(this string source)
        {
            return Encoding.GetEncoding("gb2312").GetByteCount(source.NullThenEmpty());
        }

        /// <summary>
        /// 以GB2312编码切割字符串
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="lengthGB2312">保留的长度</param>
        /// <returns></returns>
        public static string SubstringGB2312(this string source, int lengthGB2312)
        {
            var encoding = Encoding.GetEncoding("gb2312");
            source = source.NullThenEmpty();

            if (lengthGB2312 >= source.LengthGB2312())
            {
                return source;
            }

            int index = 0;
            int length = 0;
            var chars = source.ToCharArray();

            while (length < lengthGB2312)
            {
                length += encoding.GetByteCount(chars, index, 1);
                if (length == lengthGB2312)
                {
                    return new string(chars, 0, index + 1);
                }
                else if (length > lengthGB2312)
                {
                    return new string(chars, 0, index);
                }
                index++;
            }
            return string.Empty;
        }

        /// <summary>
        /// 以GB2312编码切割字符串
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="lengthGB2312">保留的长度</param>
        /// <param name="padChar">填充字符</param>
        /// <returns></returns>
        public static string SubstringGB2312(this string source, int lengthGB2312, char padChar)
        {
            source = source.NullThenEmpty().SubstringGB2312(lengthGB2312);
            int count = lengthGB2312 - source.LengthGB2312();
            if (count == 0)
            {
                return source;
            }
            return string.Concat(source, padChar.Pad(count));
        }


    }
}